package io.github.uicdb.et;

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.alchemy.PotionContents;
import net.minecraft.world.item.alchemy.Potions;
import net.minecraft.world.item.crafting.Ingredient;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.common.Tags;
import net.neoforged.neoforge.event.brewing.RegisterBrewingRecipesEvent;

import net.neoforged.neoforge.event.entity.living.LivingShieldBlockEvent;
import net.neoforged.neoforge.event.entity.player.AttackEntityEvent;
import net.neoforged.neoforge.event.tick.EntityTickEvent;

import java.util.function.Consumer;

public class GameEventHandle {
    public static void handle(IEventBus modBus, IEventBus gameBus) {

        gameBus.addListener(EntityTickEvent.Post.class, event -> {
            if (event.getEntity().level().isClientSide) return;
            var entity = event.getEntity();

            if (entity instanceof Player) {
                //攻击计数器计数
                int last = entity.getData(ModAttachments.LAST_ATTACK_TIME);
                if (last == Integer.MAX_VALUE) {
                    last = 0;
                }
                entity.setData(ModAttachments.LAST_ATTACK_TIME, last + 1);
            }
        });
        gameBus.addListener(LivingShieldBlockEvent.class, event -> {
            if (!event.isCanceled()) {
                if (event.getEntity() instanceof Player player) {
                    var stack = player.getItemInHand(player.getUsedItemHand());
                    if (stack.is(Tags.Items.TOOLS_SHIELD) && stack.has(ModItemDataComponents.ITEM_ELEMENT_DATA)) {
                        var attacker = ModHelper.findAttacker(event.getDamageContainer());
                        if (attacker != null) {
                            var itemElementData = stack.get(ModItemDataComponents.ITEM_ELEMENT_DATA);
                            if (itemElementData != null) {
                                ModHelper.attachElement(attacker, itemElementData.getElement(), itemElementData.getAmount(), itemElementData.getTicks());
                            }

                        }
                    }
                }
            }
        });
        gameBus.addListener(RegisterBrewingRecipesEvent.class, event -> {
            var builder = event.getBuilder();
            builder.addRecipe(Ingredient.of(Items.GLASS_BOTTLE), Ingredient.of(ModItems.WATER_WATER), PotionContents.createItemStack(Items.POTION, Potions.AWKWARD));
        });
        gameBus.addListener(AttackEntityEvent.class, event -> event.getEntity().setData(ModAttachments.LAST_ATTACK_TIME.get(), 0));
        gameBus.addListener(AttackEntityEvent.class, event -> {
            ItemStack mainHandItem = event.getEntity().getMainHandItem();
            if (event.getTarget() instanceof LivingEntity && mainHandItem.has(ModItemDataComponents.ITEM_ELEMENT_DATA)) {
                var data = mainHandItem.get(ModItemDataComponents.ITEM_ELEMENT_DATA);
                if (data != null)
                    ModHelper.attachElement(event.getEntity(), data.getElement(), data.getAmount(), data.getTicks());
            }
        });
    }
}
